<?php
App::uses('AppController', 'Controller');
/**
 * KursyProwadzacies Controller
 *
 * @property KursyProwadzacy $KursyProwadzacy
 */
class ProwadzacyController extends AppController {

// 	public $helpers = array('FileUpload.FileUpload');
// 	public $components = array('FileUpload.FileUpload');
	public $uses = array('KursyProwadzacy');
	
	public function beforeFilter(){
		parent::beforeFilter();
		$this->Auth->allow('index','display');
// 		$this->FileUpload->uploadDir('files'.DS.'upload'.DS.'images');
	}
	
	

	public function beforeRender(){
		parent::beforeRender();
			switch($this->action){
			case 'index':
				$this->layout = 'default';
				break;
			case 'display':
				$this->layout = 'default';
				break;
			default:
				$this->layout = 'panel';
			break;
		}
	}
	

/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->KursyProwadzacy->recursive = 0;
		$this->set('kursyProwadzacies', $this->paginate());
		$this->articles();
		
		$this->loadModel("Kursy");
		$this->Kursy->recursive = 0;
		$options['order'] = array('Kursy.start_date');
		$options['limit'] = 4;
		$options['conditions'] = array('Kursy.start_date >='=> date('Y-m-d'));
		$kursies = $this->Kursy->find('all',$options);
		$this->set('kursies', $kursies);
		
	}
	
	public function admin() {
		$this->KursyProwadzacy->recursive = 0;
		$this->set('kursyProwadzacies', $this->paginate());
		
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		
		if(isset($this->params['slug']) && !empty($this->params['slug'])){
			$kursyProwadzacy = $this->KursyProwadzacy->findBySlug($this->params['slug']);
			if (!is_array($kursyProwadzacy) || empty($kursyProwadzacy)) {
				throw new NotFoundException(__d('prowadzoacy','Invalid Prowadzacy'));
			}
		}else{
			$this->KursyProwadzacy->id = $id;
			if (!$this->KursyProwadzacy->exists()) {
				throw new NotFoundException(__d('kursy','Invalid kursy prowadzacy'));
			}
			$kursyProwadzacy = $this->KursyProwadzacy->read(null, $id);
		}
		$this->loadModel('KursyCity');
		$kursyCities = $this->KursyCity->find('list');
		$this->set('kursyCities', $kursyCities);
		$this->set('kursyProwadzacy', $kursyProwadzacy);
	}
	
	public function display($id = null) {
		$this->view($id);	
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			if($this->FileUpload->success){
				$this->request->data['KursyProwadzacy']['upload_id'] = $this->Menus->Upload->getLastInsertID();
			}else{
				// 				throw new NotFoundException($this->FileUpload->showErrors());
			}
			
			$this->request->data['KursyProwadzacy']['slug'] = strtolower( Inflector::slug($this->request->data['KursyProwadzacy']['first_name'],'-') );
			$this->KursyProwadzacy->create();
			if ($this->KursyProwadzacy->save($this->request->data)) {
				$this->Session->setFlash(__d('kursy','The kursy prowadzacy has been saved'));
				$this->redirect(array('action' => 'admin'));
			} else {
				$this->Session->setFlash(__d('kursy','The kursy prowadzacy could not be saved. Please, try again.'));
			}
		}
		$kursies = $this->KursyProwadzacy->Kursy->find('list');
		$this->set(compact('kursies'));
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->KursyProwadzacy->id = $id;
		if (!$this->KursyProwadzacy->exists()) {
			throw new NotFoundException(__d('kursy','Invalid kursy prowadzacy'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			
			if($this->FileUpload->success){
				$this->request->data['KursyProwadzacy']['upload_id'] = $this->Menus->Upload->getLastInsertID();
			}else{
				// 				throw new NotFoundException($this->FileUpload->showErrors());
			}
			
			$this->request->data['KursyProwadzacy']['slug'] = strtolower( Inflector::slug($this->request->data['KursyProwadzacy']['first_name'].'-'.$this->request->data['KursyProwadzacy']['last_name'],'-') );
			if ($this->KursyProwadzacy->save($this->request->data)) {
				$this->Session->setFlash(__d('kursy','The kursy prowadzacy has been saved'));
				$this->redirect(array('action' => 'admin'));
			} else {
				$this->Session->setFlash(__d('kursy','The kursy prowadzacy could not be saved. Please, try again.'));
			}
		} else {
			$this->request->data = $this->KursyProwadzacy->read(null, $id);
		}
		$kursies = $this->KursyProwadzacy->Kursy->find('list');
		$this->set(compact('kursies'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->KursyProwadzacy->id = $id;
		if (!$this->KursyProwadzacy->exists()) {
			throw new NotFoundException(__d('kursy','Invalid kursy prowadzacy'));
		}
		if ($this->KursyProwadzacy->delete()) {
			$this->Session->setFlash(__d('kursy','Kursy prowadzacy deleted'));
			$this->redirect(array('action' => 'admin'));
		}
		$this->Session->setFlash(__d('kursy','Kursy prowadzacy was not deleted'));
		$this->redirect(array('action' => 'admin'));
	}
}
